package com.cloudservice.ccs.biz.impl;

import com.cloudservice.ccs.biz.ISessionMessageBiz;
import com.cloudservice.ccs.entity.Session;
import com.cloudservice.ccs.entity.SessionMessage;
import com.cloudservice.ccs.exception.RoleManageException;
import com.cloudservice.ccs.exception.SessionMessageException;
import com.cloudservice.ccs.mapper.SessionMapper;
import com.cloudservice.ccs.mapper.SessionMessageMapper;
import com.cloudservice.ccs.model.ApiResult;
import com.cloudservice.ccs.websocket.SessionMessageServer;
import com.cloudservice.ccs.websocket.SessionServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
public class SessionMessageImpl implements ISessionMessageBiz {
    @Autowired
    private SessionMessageMapper sessionMessageMapper;

    @Autowired
    private SessionMapper sessionMapper;

    private static final Logger logger = LoggerFactory.getLogger(SessionMessageImpl.class);

    @Override
    public void save(SessionMessage sessionMessage) {
        int result = sessionMessageMapper.save(sessionMessage);
        if (result == 0) {
            throw new SessionMessageException("Create SessionMessage entity error");
        }
        sessionMessage = sessionMessageMapper.getOne(sessionMessage.getId());
        Integer sessionId = sessionMessage.getSessionId();
        if (sessionId != null) {
            try {
                SessionServer.sendSessionList(sessionMessage.getCustomerServiceId(), sessionMapper);
                SessionMessageServer.getCustomerServiceSessionPool().sendMessage(sessionId, ApiResult.successWithData(sessionMessage), new SimpleDateFormat("HH:mm:ss"));
                SessionMessageServer.getVisitorSessionPool().sendMessage(sessionId, ApiResult.successWithData(sessionMessage), new SimpleDateFormat("HH:mm:ss"));
            } catch (IOException e) {
                if (logger.isWarnEnabled()) {
                    logger.warn("Error when notifying new message: " + e.getClass().getName() + ": " + e.getMessage());
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void update(Integer id, SessionMessage sessionMessage) {
        int result = sessionMessageMapper.update(id, sessionMessage);
        if (result == 0) {
            throw new SessionMessageException("Update SessionMessage entity error");
        }
    }

    @Override
    public void partialUpdate(Integer id, Map<String, Object> fields) {
        int result = sessionMessageMapper.partialUpdate(id, fields);
        if (result == 0) {
            throw new SessionMessageException("Partial update SessionMessage failed");
        }
    }

    @Override
    public void delete(Integer id) {
        int result = sessionMessageMapper.delete(id);
        if (result == 0) {
            throw new SessionMessageException("Delete SessionMessage entity error");
        }
    }

    @Override
    public SessionMessage getOne(Integer id) {
        SessionMessage sessionMessage = sessionMessageMapper.getOne(id);
        if (sessionMessage == null) {
            throw new SessionMessageException("Get SessionMessage entity error: no such entity");
        }
        return sessionMessage;
    }

    @Override
    public List<SessionMessage> getAll() {
        return sessionMessageMapper.getAll();
    }

    @Override
    public List<SessionMessage> getAllInSession(Integer sessionId) {
        List<SessionMessage> sessionMessages = sessionMessageMapper.getAllInSession(sessionId);
        Collections.reverse(sessionMessages);
        return sessionMessages;
    }

    @Override
    public List<SessionMessage> getPreviousInSession(Integer sessionId, Integer id) {
        List<SessionMessage> sessionMessages = sessionMessageMapper.getPreviousInSession(sessionId, id);
        Collections.reverse(sessionMessages);
        return sessionMessages;
    }
}
